package com.flag.liutao.common.domain;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author liutao
 */
@Data
public class ResponseResult<T> implements Serializable {
    private int status;

    private boolean success;

    private String msg;

    private T data;

    @JSONField(
            format = "yyyy-MM-dd HH:mm:ss"
    )
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private Date time;

    public ResponseResult(int status,boolean success,String msg,T data){
        this.data = data;
        this.status = status;
        this.success = success;
        this.msg = msg;
        this.time = new Date();
    }

    public ResponseResult(int status,boolean success,String msg){
        this.status = status;
        this.success = success;
        this.msg = msg;
        this.time = new Date();
    }

    public ResponseResult(){
        this.time = new Date();
    }



    public static <T> ResponseResult<T> result(ApiCode apiCode, String message, T data) {
        boolean success = false;
        if (apiCode.getCode() == ApiCode.SUCCESS.getCode()) {
            success = true;
        }

        if (StrUtil.isBlank(message)) {
            message = apiCode.getMessage();
        }
        ResponseResult result = new ResponseResult(apiCode.getCode(),success,message,data);
        return result;
    }

    public static ResponseResult<Boolean> ok() {
        return result(ApiCode.SUCCESS,null,null);
    }

    public static <T> ResponseResult<T> ok(T data) {
        return result(ApiCode.SUCCESS,null, data);
    }

    public static <T> ResponseResult<T> ok(T data, String message) {
        return result(ApiCode.SUCCESS, message, data);
    }

    public static ResponseResult<Map<String, Object>> okMap(String key, Object value) {
        Map<String, Object> map = new HashMap(1);
        map.put(key, value);
        return ok(map);
    }

    public static ResponseResult<Boolean> fail(ApiCode apiCode) {
        return result(apiCode, null,null);
    }

    public static ResponseResult<String> fail(String message) {
        return result(ApiCode.FAIL, message,null);
    }

    public static <T> ResponseResult<T> fail(ApiCode apiCode, T data) {
        if (ApiCode.SUCCESS == apiCode) {
            throw new RuntimeException("失败结果状态码不能为" + ApiCode.SUCCESS.getCode());
        } else {
            return result(apiCode,null, data);
        }
    }

    public static ResponseResult<String> fail(Integer errorCode, String message) {
        ResponseResult result = new ResponseResult();
        result.setStatus(errorCode);
        result.setMsg(message);
        return result;
    }

    public static ResponseResult<Map<String, Object>> fail(String key, Object value) {
        Map<String, Object> map = new HashMap(1);
        map.put(key, value);
        return result(ApiCode.FAIL,null, map);
    }

    public static ResponseResult<Boolean> fail() {
        return fail(ApiCode.FAIL);
    }





}